package com.client.rule;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.user.client.Window;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.shared.data.IData;
import com.shared.rule.TypeDataRule;
import com.shared.rule.TypeDecision;

public class UiDecision extends UiRule{
	
	
	private TypeDecision typeDecision;
	private Map<String,String> decisionData = new HashMap<String, String>();
	
	public UiDecision(){
		super();
	}
	
	public UiDecision(String label){
		this();
		this.label = label;
		setText(label);
	}
	
	public UiDecision(String label,TypeDecision typeDecision){
		this(label);
		
		this.typeDecision = typeDecision;
	}
	
	public UiDecision(String label,TypeDecision typeDecision,Map<String,String> decisionData){
		this(label);		
		this.typeDecision = typeDecision;
		this.decisionData = decisionData;
	}
	
	/**
	 * @return the decisionData
	 */
	public Map<String, String> getDecisionData() {
		return decisionData;
	}

	/**
	 * @param decisionData the decisionData to set
	 */
	public void setDecisionData(Map<String, String> decisionData) {
		this.decisionData = decisionData;
	}

	

	@Override
	public void generateFromXML(Node xmlNode) {
		Element ruleElement = (Element) xmlNode;		
		label = ruleElement.getAttribute("label");
		typeDecision = TypeDecision.getTypeDecision(ruleElement.getAttribute("type"));
		setText(label);
		
		Node n = xmlNode.getFirstChild();
		Element rElement = (Element) n;	
		for(int i=0;i<rElement.getAttributes().getLength();i++){			
			decisionData.put(rElement.getAttributes().item(i).getNodeName(), rElement.getAttribute(rElement.getAttributes().item(i).getNodeName()));
		}		
	}

	@Override
	public boolean isComposite() {
		return false;
	}

	@Override
	public String toXml(Map<String,TypeDataRule> mapData) {
		
		String xml = "<Rule type=\""+typeDecision.toString()+"\" label=\""+label+"\">";		
		xml = xml+"<"+typeDecision.toString();
		for(String key : decisionData.keySet()) xml = xml+" "+key+"=\""+decisionData.get(key)+"\" ";
		xml = xml+"/></Rule>";		
		return xml;
	}
	

	@Override
	public void getUsedDataXml(Map<String, String> usedData,Map<String,TypeDataRule> mapData) {
		
	}

	/**
	 * @param typeDecision the typeDecision to set
	 */
	public void setTypeDecision(TypeDecision typeDecision) {
		this.typeDecision = typeDecision;
	}

	/**
	 * @return the typeDecision
	 */
	public TypeDecision getTypeDecision() {
		return typeDecision;
	}

	
}
